package org.yang.order.food.service.impl;


import com.kuding.anno.ExceptionLinstener;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.yang.order.food.common.Msg;
import org.yang.order.food.common.MessageConfig;
import org.yang.order.food.dao.FoodMapper;
import org.yang.order.food.entity.Food;
import org.yang.order.food.service.FoodService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 食物服务实现
 * @author shichenyang
 * @email gyv12345@163.com
 * @date 2018/12/03
 */
@Service
@Transactional(rollbackFor = Exception.class)
@ExceptionLinstener
public class FoodServiceImpl implements FoodService {
    @Resource
    private FoodMapper foodMapper;

    @CacheEvict(cacheNames = "foods",allEntries = true)
    @Override
    public Msg addFood(Food food) {
        food.setCount(0);
        food.setPraise(0);
        int i=  foodMapper.insert(food);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg(MessageConfig.ADD_SUCCESS);
        }else{
            msg.setCode(-1);
            msg.setMsg(MessageConfig.ADD_ERR);
        }
        return msg;
    }

    @Override
    public Msg updateFood(Food food) {
        int i= foodMapper.updateByParam(food);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg(MessageConfig.UPDATE_SUCCESS);
        }else{
            msg.setCode(-1);
            msg.setMsg(MessageConfig.UPDATE_ERR);
        }
        return msg;
    }

    @Override
    public Msg delFood(Food food) {
        int i=foodMapper.del(food.getId());
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg(MessageConfig.DEL_SUCCESS);
        }else{
            msg.setCode(-1);
            msg.setMsg(MessageConfig.DEL_ERR);
        }
        return msg;
    }

    @Override
    public PageInfo getFoods(Food food, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Food> foods=foodMapper.list(food);
        PageInfo info=new PageInfo(foods);
        info.getList();
        return info;
    }

    @Cacheable(cacheNames = "foods")
    @Override
    public List<Food> getAll() {
        return foodMapper.list(null);
    }
}
